﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
using System.Collections;
using System.Xml.Serialization;

namespace OrderM
{
    public class OrderService
    {
        public List<Order> orders = new List<Order>();
        public List<Order> OrderInq = new List<Order>();
        public bool AddOrder(Order order)
        {

            bool flag = true;

            foreach (Order item in orders)
            {
                flag = order.Equals(item);
                Console.WriteLine(flag);
                if (flag)
                {
                    //Console.WriteLine("Fail added.Repeated order");
                    return false;
                }
            }
            orders.Add(order);
            //Console.WriteLine("Successfully Add Order");
            return true;
        }

       

        public void DelOrder(int id)
        {
            for (int i = 0; i < orders.Count(); i++)
                if (orders[i].Id == id)
                {
                    orders.Remove(orders[i]);
                }
        }
        public bool InqOrder(int id)
        {
            var query = from o in orders where o.Id == id orderby o.price select o;
            OrderInq.Clear();
            OrderInq = query.ToList();
            if (OrderInq == null)
            {
                //Console.WriteLine("Order doesn't exist");
                return false;
            }

            else
            {
                //foreach (Order o in OrderInq)
                    //Console.WriteLine(o.ToString());
                return true;
            }

        }
        public bool InqOrder(int choice, string NameOrType)
        {
            OrderInq.Clear();
            switch (choice)
            {
                case 2:
                    {
                        var query = from o in orders where o.customer == NameOrType orderby o.price select o;

                        OrderInq = query.ToList();
                        if (OrderInq == null)
                        {
                            Console.WriteLine("Order doesn't exist");
                            return false;
                        }
                        else
                        {
                            foreach (Order o in OrderInq)
                                Console.WriteLine(o.ToString());
                            return true;
                        }
                    }
                case 3:
                    {
                        var query = from o in orders where o.StuffType == NameOrType orderby o.price select o;
                        OrderInq = query.ToList();
                        if (OrderInq == null)
                        {
                            Console.WriteLine("Order doesn't exist");
                            return false;
                        }
                        else
                        {
                            foreach (Order o in OrderInq)
                                Console.WriteLine(o.ToString());
                            return true;
                        }
                    }
                default: return false;
            }
        }
        public bool InqOrder(double price)
        {
            OrderInq.Clear();
            var query = from o in orders where o.price == price orderby o.price select o;
            OrderInq = query.ToList();
            if (OrderInq == null)
            {
                Console.WriteLine("order doesn't exist");
                return false;
            }
            else
            {
                foreach (Order o in OrderInq)
                    Console.WriteLine(o.ToString());
                return true;
            }
        }

        public void EditOrder(Order o, int newID)
        {
            DelOrder(o.Id);
            Order temp = o;
            o.Id = newID;
            bool flag = false;
            foreach (Order item in orders)
            {
                flag = o.Equals(item);
                Console.WriteLine(flag);
                if (flag)
                {
                    Console.WriteLine("Fail Edit.Repeated order");
                    AddOrder(temp);
                    return;
                }
            }
            AddOrder(o);
            Console.WriteLine("updated order:" + o.ToString());
        }
        public void EditOrder(Order o, string NameorType, int choice)
        {
            DelOrder(o.Id);
            Order temp = o;
            switch (choice)
            {
                case 2:
                    {
                        o.customer = NameorType;
                        bool flag = false;
                        foreach (Order item in orders)
                        {
                            flag = o.Equals(item);
                            Console.WriteLine(flag);
                            if (flag)
                            {
                                Console.WriteLine("Fail Edit.Repeated order");
                                AddOrder(temp);
                                return;
                            }
                        }
                        AddOrder(o);
                        Console.WriteLine("updated order:" + o.ToString());
                        break;
                    }

                case 4:
                    {
                        o.StuffType = NameorType;

                        bool flag = false;
                        foreach (Order item in orders)
                        {
                            flag = o.Equals(item);
                            Console.WriteLine(flag);
                            if (flag)
                            {
                                Console.WriteLine("Fail Edit.Repeated order");
                                AddOrder(temp);
                                return;
                            }
                        }
                        AddOrder(o);
                        Console.WriteLine("updated order:" + o.ToString());
                        break;
                    }
                default: break;
            }
        }
        //public void EditOrder(Order o, string newPrice)
        //{
        //    DelOrder(o.Id);
        //    Order temp = o;
        //    o.price = newPrice;
        //    bool flag = false;
        //    foreach (Order item in orders)
        //    {
        //        flag = o.Equals(item);
        //        Console.WriteLine(flag);
        //        if (flag)
        //        {
        //            Console.WriteLine("Fail Edit.Repeated order");
        //            AddOrder(temp);
        //            return;
        //        }
        //    }
        //    AddOrder(o);
        //    Console.WriteLine("updated order:" + o.ToString());
        //}

        public List<Order> SortOrder(int choice)
        {
            switch (choice)
            {
                case 0:
                    {
                        //Console.WriteLine("orders sorted by order id:");
                        var query = from o in orders orderby o.Id select o;                      
                        List<Order> ord = query.ToList();
                        
                        foreach (Order order in ord)
                            Console.WriteLine(order.ToString());
                        return ord;
                    }
                case 1:
                    {
                        //Console.WriteLine("orders sorted by total price");
                        var query1 = from o in orders orderby o.price select o;
                        List<Order> ord1 = query1.ToList();
                        
                        foreach (Order order in ord1)
                            Console.WriteLine(order.ToString());
                        return ord1;
                    }
                default:return null;

            }
           
        }

        public bool Export()
        {
            var reader = new XmlSerializer(typeof(List<Order>));
            //写入文件
            using (var fs = File.OpenWrite(@"DataFile.xml"))
            {
                reader.Serialize(fs, orders);
            }
            //reader.Serialize(Console.Out, orders);
           // Console.WriteLine("Successfully export");


            return true;
        }

        public bool Import()
        {
            var reader = new XmlSerializer(typeof(List<Order>));
            using (var fs = File.OpenRead(@"DataFile.xml"))
            {

                var result = reader.Deserialize(fs) as List<Order>;
                foreach (Order item in result)
                {
                    bool flag = true;
                    foreach (Order o in orders)
                    {
                        if (o.Equals(item))
                        {
                            flag = false;
                        }


                    }
                    if (flag)
                        orders.Add(item);
                }

                //result.ForEach(Console.WriteLine);
            }
            //Console.WriteLine("Successfully import");
            return true;

        }
    }


}
